package info.androidhive.slidingmenu;

import android.app.Fragment;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.text.DecimalFormat;
import java.util.ArrayList;

import com.dongpang.domain.ArrayLte;
import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener;
import com.handmark.pulltorefresh.library.PullToRefreshBase.State;
import com.handmark.pulltorefresh.library.extras.SoundPullEventListener;

import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ListView;

public class LteFragment extends Fragment {

	DecimalFormat df = new DecimalFormat("###,###");
	
	static class ViewHolder {
		TextView regdate;
		TextView point;
		TextView totpoint;
		TextView getoruseC;
		int position;
	}

	public LteFragment(){
		
	}
	
	ArrayList<ArrayLte> datas = new ArrayList<ArrayLte>();

	ListView list;
    MyAdapter adapter;
    private PullToRefreshListView mPullRefreshListView;
	
	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		getActivity().setTitle("LTE");
        View rootView = inflater.inflate(R.layout.pull_refresh, container, false);
		mPullRefreshListView = (PullToRefreshListView) rootView.findViewById(R.id.pull_refresh_list);
		mPullRefreshListView.setOnRefreshListener(new OnRefreshListener<ListView>() {
			@Override
			public void onRefresh(PullToRefreshBase<ListView> refreshView) {
				datas.clear();
				new JsonReadTask().execute(null, null, null);
			}
		});

		SoundPullEventListener<ListView> soundListener = new SoundPullEventListener<ListView>(getActivity());
		soundListener.addSoundEvent(State.PULL_TO_REFRESH, R.raw.pull_event);
		soundListener.addSoundEvent(State.RESET, R.raw.reset_sound);
		soundListener.addSoundEvent(State.REFRESHING, R.raw.refreshing_sound);
		mPullRefreshListView.setOnPullEventListener(soundListener);

		adapter = new MyAdapter(getActivity(), R.layout.lte_row, datas);
		mPullRefreshListView.setAdapter(adapter);

		if (datas == null || datas.size() == 0) {
			new JsonReadTask().execute(null, null, null);
		}
        
        return rootView;
    }
	
	
	class JsonReadTask extends AsyncTask<Void, Void, Void> {

		@Override
		protected void onPreExecute() {
			super.onPreExecute();
		}

		@Override
		protected Void doInBackground(Void... params) {

			String jUrl = "http://dongpang.co.kr/android/admin.v02/lte.php";
			String str = JSONfunctions.getStringfromURL(jUrl);
			Gson gson = new Gson();
			ArrayLte[] datas2 = gson.fromJson(str, ArrayLte[].class);
			for (ArrayLte data : datas2) {
				datas.add(data);
			}	
			
			return null;
		}

		@Override
		protected void onPostExecute(Void result) {
			mPullRefreshListView.onRefreshComplete();
			adapter.notifyDataSetChanged();
			super.onPostExecute(result); 
		}

	}		
	
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		super.onActivityResult(requestCode, resultCode, data);
	}

	class MyAdapter extends ArrayAdapter<ArrayLte> {
		
		protected static final int REQUEST_DETAIL = 0;

		public MyAdapter(Context context, int resource, ArrayList<ArrayLte> items) {
			super(context, resource, items);
		}

		@Override
		public View getView(final int position, View convertView, ViewGroup parent) {

			if (convertView == null) {
				convertView = getActivity().getLayoutInflater().inflate(R.layout.lte_row, null);
			}
			
			
	    	((TextView)convertView.findViewById(R.id.orddt)).setText(getItem(position).orddt);
	    	((TextView)convertView.findViewById(R.id.corpName)).setText(getItem(position).corpName);
	    	((TextView)convertView.findViewById(R.id.address)).setText(getItem(position).address);
	    	((TextView)convertView.findViewById(R.id.exaddress)).setText(getItem(position).exaddress);
	    	
	    	if(getItem(position).transFastPart.equals("1")){
	    		((ImageView)convertView.findViewById(R.id.transFastPart)).setImageDrawable(getResources().getDrawable(R.drawable.trans_fast1));
	    	}else{
	    		((ImageView)convertView.findViewById(R.id.transFastPart)).setImageDrawable(getResources().getDrawable(R.drawable.trans_fast2));
	    	}

			convertView.setOnClickListener(new View.OnClickListener() {

				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					
					Intent intent = new Intent(getActivity(), LteDetail.class);
					intent.putExtra("ordno", getItem(position).ordno);
					startActivityForResult(intent, 0);

				}
				
			});
			
			return convertView;
		}

	}	
	
	
	
	
	
	
}
